//prepare state IIM moments
use cz two_par_pooled_pooled_p25 using "$data/Opp_Atlas/cz_outcomes", clear
save "$temp/p25_twopar", replace

//open other data and merge on
use "$data/LOO/onlinedata5", clear
keep cz e_rank_b_8082
ren e iim
merge 1:1 cz using "$temp/p25_twopar", keep(match) nogen //married parent rate
merge 1:1 cz using "$data/LOO/onlinedata8" //covariates
keep iim two_par stateab pop2000 //restrict

//clean population varaible
replace pop2000 = subinstr(pop2000, ", ", "", .)
replace pop2000 = subinstr(pop2000, ",", "", .)
replace pop2000 = subinstr(pop2000, " ", "", .)
replace pop2000 = trim(pop2000)
destring pop2000, replace

//collapse
collapse (mean) iim two_par [fw = pop2000], by(state)
su iim
local iim_mean = `r(mean)'
local iim_sd = `r(sd)'
su iim
corr iim two_par*
local marr_cor = `r(rho)'

ren state state
merge 1:1 state using "$data/Crosswalks/state_fips_crosswalk", keep(match) nogen
keep statefips iim two_par
save "$temp/chetty_state_estimates", replace

//add to moments and export
use "$temp/moments_withtime", clear
count 
local new = `r(N)' + 2
set obs `new'
local line1 = `r(N)' + 1
local line2 = `r(N)' + 2
//local line3 = `r(N)' + 3
replace moment = `iim_mean' in `line1'
replace moment = `iim_sd' in `line2'
//replace moment = `marr_cor' in `line3'
save "$temp/moments_withiim", replace

//IIM moment standard error, from CHKS and decreased a bit to avoid completely dominating objective function
clear
set obs 1
gen float v1 = 11111111.11
export delimited "$dir/Model/boot/iim_se.csv", novarn replace
clear



//end of dofile
